package com.itheima.sc.service.order;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.order.OmsOrderReturnReasonApi;
import com.itheima.sc.pojo.order.OmsOrderReturnReason;
import com.itheima.sc.result.Result;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@Service
public class OmsOrderReturnReasonService {
    @DubboReference
    OmsOrderReturnReasonApi omsOrderReturnReasonApi;

    public Result listOmsOrder(Integer page, Integer size) {        //订单列表
        Page<OmsOrderReturnReason> omsOrders = omsOrderReturnReasonApi.listOmsOrderReturnApply(page, size);
        PageResult pageResult = new PageResult(page, size, Convert.toInt(omsOrders.getTotal()),Convert.toInt(omsOrders.getPages()), omsOrders.getRecords());
        return Result.success(pageResult);
    }

    public void create(Map<String, String> map) {
        OmsOrderReturnReason omsOrderReturnReason = new OmsOrderReturnReason();
        try {
            omsOrderReturnReason.setId(Convert.toLong(map.get("id")));
            String id = map.get("id");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            Date date = simpleDateFormat.parse(simpleDateFormat.format(new Date(System.currentTimeMillis())));
            omsOrderReturnReason.setCreateTime(date);
            omsOrderReturnReason.setName(map.get("name"));
            omsOrderReturnReason.setSort(Convert.toInt(map.get("sort")));
            omsOrderReturnReason.setStatus(Convert.toInt(map.get("status")));
            omsOrderReturnReasonApi.create(omsOrderReturnReason);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void deletByIds(ArrayList<Long> ids) {

        if (ObjectUtil.isEmpty(ids) || ids.size() == 0) {
            return;
        }
        omsOrderReturnReasonApi.deletByIds(ids);

    }

    public void updateStatus(ArrayList<Long> ids, Map<String, String> map) {
        if (ObjectUtil.isEmpty(ids) || ids.size() == 0) {
            return;
        }
        Integer status = Convert.toInt(map.get("status"));
        omsOrderReturnReasonApi.updateStatus(ids,status);


    }

    public void updateReason(Long id, OmsOrderReturnReason omsOrderReturnReason) {
        omsOrderReturnReasonApi.updateReason(id,omsOrderReturnReason);
    }

    public Result getById(Long id) {
        OmsOrderReturnReason omsOrderReturnReason=omsOrderReturnReasonApi.getReason(id);
        return Result.success(omsOrderReturnReason);
    }
}
